package com.huishen.edrivenew.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.huishen.edrivenew.R;
import com.huishen.edrivenew.bean.CoaLessonInfoBean;
import com.huishen.edrivenew.bean.LessInfoBean;

/**
 * 约课界面下拉列表适配器
 * TODO 修改为base
 */
public class AppointmentSubExListApdater implements ExpandableListAdapter{
	
	private Context mContext;
	private LayoutInflater mInflater = null;
	private TimeClassApointListener listener ;

	private CoaLessonInfoBean coaLessonInfoBean;
	private List<LessInfoBean> groupBean = new ArrayList<LessInfoBean>();
	private List<LessInfoBean> list1Bean = new ArrayList<LessInfoBean>();
	private List<LessInfoBean> list2Bean = new ArrayList<LessInfoBean>();





	public AppointmentSubExListApdater(Context ctx, CoaLessonInfoBean coaLessonInfoBean,
									   TimeClassApointListener listener) {
		mContext = ctx;
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.coaLessonInfoBean = coaLessonInfoBean;
		this.listener = listener ;

		coaLessonInfoBean.parseLesson();

        setData();
	}

    public void setData() {
        this.groupBean.clear();
        this.list1Bean.clear();
        this.list2Bean.clear();
        this.groupBean.addAll(coaLessonInfoBean.groupBean);
        this.list1Bean.addAll(coaLessonInfoBean.listkm2);
        this.list2Bean.addAll(coaLessonInfoBean.listkm3);
    }



	@Override
	public boolean areAllItemsEnabled() {
		
		return false;
	}

	@Override
	public LessInfoBean getChild(int groupPosition, int childPosition) {
        try {
            if(groupPosition == 0) {
                return list1Bean.get(childPosition);
            } else if(groupPosition == 1) {
                return list2Bean.get(childPosition);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}


	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {

        GroupViewHolder group = null ;
		if (convertView == null) {
			convertView = mInflater
					.inflate(R.layout.item_appointment_sub, null);
			group = new GroupViewHolder();
			group.subName = (TextView)convertView.findViewById(R.id.sub_name) ;
			group.subStatus = (TextView)convertView.findViewById(R.id.sub_status) ;
			group.subIcon = (ImageView)convertView.findViewById(R.id.sub_ic) ;
			convertView.setTag(group) ;
		}else{
			group = (GroupViewHolder)convertView.getTag() ;
		}
		if (isExpanded) {
			group.subIcon.setImageResource(R.drawable.ic_buttom);
		} else {
			group.subIcon.setImageResource(R.drawable.ic_next);
		}

//		// ----------------添加列表数据------------------
		LessInfoBean gBean = groupBean.get(groupPosition);
        group.subName.setText(gBean.name);

        group.subStatus.setText(gBean.count + "/" + gBean.kmcount);
		return convertView;
	}
//

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildViewHolder holder = null ;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_appointment_class,
					null);
			holder = new ChildViewHolder();
			holder.classTime = (TextView) convertView.findViewById(R.id.class_time) ;
			holder.classStatus = (TextView) convertView.findViewById(R.id.class_status) ;
			holder.classBtn = (Button) convertView.findViewById(R.id.class_btn) ;
			convertView.setTag(holder);
		}else{
			holder = (ChildViewHolder)convertView.getTag() ;
		}
//		// ----------------添加列表数据------------------
        LessInfoBean subBean = null;
        if(groupPosition ==0) {
            subBean = list1Bean.get(childPosition);
        } else if(groupPosition == 1) {
            subBean = list2Bean.get(childPosition);
        }
        if(subBean == null) {
            return convertView;
        }

        holder.classTime.setText(subBean.name);
        holder.classStatus.setText(subBean.count + "/" + subBean.kmcount);
		holder.classBtn.setEnabled(true);

		if(subBean.status == 1){//已约 橙色
			holder.classBtn.setBackgroundResource(R.drawable.item_appointment_day_exists);
			holder.classBtn.setText("取消");
		}else if(subBean.status == 2){ //已满 灰色
			holder.classBtn.setBackgroundResource(R.drawable.bindcoach_appoint_day_exist_circle);
			holder.classBtn.setText("已满");
			holder.classBtn.setEnabled(false);
		}else{//可约 绿色
			holder.classBtn.setBackgroundResource(R.drawable.item_appointment_day_full);
			holder.classBtn.setText("预约");
			
		}

        final LessInfoBean finalSubBean = subBean;
        holder.classBtn.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
//				arg0.setEnabled(false);
				listener.setOnClick(finalSubBean.subject, finalSubBean.timeType, finalSubBean.status);
			}
		});
//		// ----------------添加列表数据结束！----------------
		return convertView;
	}
	
	private class GroupViewHolder {
		TextView subName;
		TextView subStatus;
		ImageView subIcon;
	}

	private class ChildViewHolder {
		TextView classTime;
		TextView classStatus;
		Button classBtn;
	}

	@Override
	public int getChildrenCount(int arg0) {
		if(arg0 ==0) {
            return list1Bean.size();
        } else {
            return list2Bean.size();
        }
	}

	@Override
	public long getCombinedChildId(long arg0, long arg1) {
		
		return 0;
	}

	@Override
	public long getCombinedGroupId(long arg0) {
		
		return 0;
	}

	@Override
	public Object getGroup(int arg0) {
		return groupBean.get(arg0);
	}

	@Override
	public int getGroupCount() {
		return groupBean.size();
	}

	@Override
	public long getGroupId(int position) {
		return position;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}

	@Override
	public boolean isEmpty() {
		return false;
	}

	@Override
	public void onGroupCollapsed(int arg0) {
	}

	@Override
	public void onGroupExpanded(int arg0) {
	}

	@Override
	public void registerDataSetObserver(DataSetObserver arg0) {
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver arg0) {
	}



}
